<?php
class api_handler extends spController {
	
	private $request_data;
	function login() {
		$response_data = array ('error'=>'');
		$userObj = spClass ( "model_user" );
		$uname = $this->spArgs ("uname");
		$key = $this->spArgs ( "key" );
		try {
			if ( !$key ) 
				throw new Exception("can not find key parameter");
			if( !$uname )
				throw new Exception("can not find uname parameter");
			
			if ( $key !== constant('API_KEY') )
				throw new Exception("key incorrect");
			
			$upass = md5 ( $this->spArgs ( "upass" ) );
			$userObj = spClass ( "model_user" );
			// validate
			$rows = array ('uname' => $uname, 'upass' => $upass );
			if ( false == $userObj->userlogin($uname, $upass) ) {
				throw new Exception("用户名或者密码错误");
			} else {
				$response_data['data'] = session_id();
			}
		}catch (Exception $e){
			$response_data['error'] = $e->getMessage();
		}
		
		echo json_encode($response_data);
	}
	function getdata(){
		$table_name = $this->spArgs("tbName");
		$table_name = strtolower($table_name);
		$current_version_number = $this->spArgs("version");
		$model_obj = spClass('model_'.$table_name);
		
		$conditions = sprintf("`version_id` > %d",$current_version_number);
		$records_list = $model_obj->findAll($conditions,"version_id ASC");
		
		foreach($records_list as &$ref_row_obj) {
			$ref_row_obj['active'] = $ref_row_obj['active'] == 'Y'?1:0;
			if ( 'resource' == $table_name ) {
				
				$ref_row_obj['downloaded'] = '0';
				$file_path = ASSETS_DIR.'/'.$ref_row_obj['path'];
				if ( file_exists($file_path) ) {
					$ref_row_obj['size'] = filesize($file_path);
				} else {
					$ref_row_obj['size'] = 0;
				}
			}
		}
		$response_data = array('error'=>'');
		$response_data['data'] = $records_list;
		echo json_encode($response_data);
		exit();
	}
	function download_resource(){
		$uuid = $this->spArgs("uuid");
		$model_obj = spClass("model_resource");
		$resource_data = $model_obj->get($uuid);
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=".$resource_data['path']);
		readfile(ASSETS_DIR.'/'.$resource_data['path']);
	}
	function need_sync_list() {
		$v_beverage = $this->spArgs("v_beverage");
		$v_beverage_item = $this->spArgs("v_beverage_item");
		$v_food = $this->spArgs("v_food");
		$v_food_item = $this->spArgs("v_food_item");
		
		$v_ibizatv = $this->spArgs("v_ibizatv");
		$v_slider_image = $this->spArgs("v_slider_image");
		$v_resource = $this->spArgs("v_resource");
		$v_delete = $this->spArgs("v_delete");
		
		$response_data = array('error'=>'','date'=>array());
		if ( $this->is_table_need_sync('beverage',$v_beverage) ) {
			$response_data['data'][] = 'beverage';
		}
		if ( $this->is_table_need_sync('beverage_item',$v_beverage_item) ) {
			$response_data['data'][] = 'beverage_item';
		}
		if ( $this->is_table_need_sync('food',$v_food) ) {
			$response_data['data'][] = 'food';
		}
		if ( $this->is_table_need_sync('food_item',$v_food_item) ) {
			$response_data['data'][] = 'food_item';
		}
		if ( $this->is_table_need_sync('ibizatv',$v_ibizatv) ) {
			$response_data['data'][] = 'ibizatv';
		}
		if ( $this->is_table_need_sync('slider_image',$v_slider_image) ) {
			$response_data['data'][] = 'slider_image';
		}
		if ( $this->is_table_need_sync('resource',$v_resource) ) {
			$response_data['data'][] = 'resource';
		}
		if ( $this->is_table_need_sync('delete_record_log',$v_delete) ) {
			$response_data['data'][] = 'delete';
		}
		echo json_encode($response_data);
		exit();
	}
	
	private function is_table_need_sync($tableName,$version) {
		$sql = sprintf("SELECT MAX(`version_id`) AS `max_version` FROM `%s`",$tableName);
		$result = mysql_query($sql);
		$row = mysql_fetch_assoc($result);
		return $row['max_version'] > $version;
	}
}

?>